package yx.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import yx.dao.BaseDaoInt;
import yx.model.BsAddrbook;
import yx.model.BsCorporation;
import yx.model.BsStudent;
import yx.model.SmCode;
import yx.vo.AddrbookVO;
import yx.vo.DatagridVO;

@Service("classAddrbookService")
public class ClassAddrbookServiceImpl implements ClassAddrbookServiceInt {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ClassAddrbookServiceImpl.class);

	private BaseDaoInt<BsAddrbook> addrbookDao;
	private BaseDaoInt<BsStudent> studentDao;
	private BaseDaoInt<SmCode> codeDao;

	public BaseDaoInt<BsAddrbook> getAddrbookDao() {
		return addrbookDao;
	}

	@Autowired
	public void setAddrbookDao(BaseDaoInt<BsAddrbook> addrbookDao) {
		this.addrbookDao = addrbookDao;
	}

	public BaseDaoInt<BsStudent> getStudentDao() {
		return studentDao;
	}

	@Autowired
	public void setStudentDao(BaseDaoInt<BsStudent> studentDao) {
		this.studentDao = studentDao;
	}

	public BaseDaoInt<SmCode> getCodeDao() {
		return codeDao;
	}

	@Autowired
	public void setCodeDao(BaseDaoInt<SmCode> codeDao) {
		this.codeDao = codeDao;
	}

	@Override
	public void sync() {
		String hql = "from BsStudent t, BsCorporation c where t.corporationId = c.id";
		List list = studentDao.find(hql);
		for (int i = 0; i < list.size(); i++) {
			Object[] obj = (Object[]) list.get(i);
			BsStudent stu = (BsStudent) obj[0];
			BsCorporation corp = (BsCorporation) obj[1];

			BsAddrbook addrbook = new BsAddrbook();
			addrbook.setAddrbookType("02");
			addrbook.setName(stu.getStudentName());
			addrbook.setGender(stu.getGender());
			addrbook.setPosition(stu.getPosition());
			addrbook.setCorporationName(corp.getCorporationName());
			addrbook.setMobile(stu.getMobile());
			addrbook.setQq(stu.getQq());
			addrbook.setEmail(stu.getEmail());
			addrbook.setPhone(stu.getPhone());
			addrbook.setFax(stu.getFax());

			addrbookDao.save(addrbook);
		}
	}

	@Override
	public DatagridVO datagrid(AddrbookVO addrbook) {
		DatagridVO dg = new DatagridVO();
		String hql = "from BsAddrbook t where t.addrbookType = '02'";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(addrbook, hql, params);
		String totalHql = "select count(*) " + hql;
		hql = addOrder(addrbook, hql);
		List<BsAddrbook> l = addrbookDao.find(hql, params, addrbook.getPage(), addrbook.getRows());
		List<AddrbookVO> nl = new ArrayList<AddrbookVO>();
		changeModel(l, nl);
		dg.setTotal(addrbookDao.count(totalHql, params));
		dg.setRows(nl);
		return dg;
	}

	private void changeModel(List<BsAddrbook> l, List<AddrbookVO> nl) {
		if (l != null && l.size() > 0) {
			Map<String, String> retMap = getGenderMap("02");
			for (BsAddrbook addrbook : l) {
				AddrbookVO u = new AddrbookVO();
				BeanUtils.copyProperties(addrbook, u);
				// 性别
				if (addrbook.getGender() != null && !"".equals(addrbook.getGender())) {
					u.setGenderName(retMap.get(addrbook.getGender()));
				}
				nl.add(u);
			}
		}
	}

	private String addOrder(AddrbookVO addrbook, String hql) {
		if (addrbook.getSort() != null || addrbook.getOrder() != null) {
			hql += " order by " + addrbook.getSort() + " " + addrbook.getOrder();
		}
		return hql;
	}

	private String addWhere(AddrbookVO addrbook, String hql, Map<String, Object> params) {
		if (addrbook.getName() != null && !"".equals(addrbook.getName().trim())) {
			hql += " and t.name like :name";
			params.put("name", "%%" + addrbook.getName().trim() + "%%");
		}
		if (addrbook.getMobile() != null && !"".equals(addrbook.getMobile().trim())) {
			hql += " and t.mobile like :mobile";
			params.put("mobile", "%%" + addrbook.getMobile().trim() + "%%");
		}
		if (addrbook.getPhone() != null && !"".equals(addrbook.getPhone().trim())) {
			hql += " and t.phone like :phone";
			params.put("phone", "%%" + addrbook.getPhone().trim() + "%%");
		}
		if (addrbook.getQq() != null && !"".equals(addrbook.getQq().trim())) {
			hql += " and t.qq like :qq";
			params.put("qq", "%%" + addrbook.getQq().trim() + "%%");
		}
		if (addrbook.getEmail() != null && !"".equals(addrbook.getEmail().trim())) {
			hql += " and t.email like :email";
			params.put("email", "%%" + addrbook.getEmail().trim() + "%%");
		}
		return hql;
	}

	// 性别
	private Map<String, String> getGenderMap(String type) {
		Map<String, String> retMap = new HashMap<String, String>();
		if (type != null && !"".equals(type)) {
			String hql = "from SmCode t where t.ctype = :ctype";
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("ctype", type);
			List<SmCode> l = codeDao.find(hql, params);
			for (SmCode c : l) {
				retMap.put(c.getCvalue(), c.getCname());
			}
		}
		return retMap;
	}
}